<?php

App::import('Sanitize');

class FriendsController extends AppController{

	var $helpers = array('Html', 'Form');
	var $name = 'Friends';
	var $uses = array('Message', 'User', 'Friend', 'Invite', 'RequestFriend', 'Post');
	
	function index() {
		$user = $this->Session->read('user');
		$friends_id_1 = $this->Friend->query("SELECT user2_id AS id FROM friends WHERE user1_id = " . $user['User']['id'], $cachequeries = false);
		$friends_id_2 = $this->Friend->query("SELECT user1_id AS id FROM friends WHERE user2_id = " . $user['User']['id'], $cachequeries = false);
		$friends_id = array_merge($friends_id_1, $friends_id_2);
		
		$friends = array();      
		foreach ($friends_id as $friend_id) {
			$friends[] = $this->User->find('first', array('conditions'=>array('User.id'=>$friend_id['friends']['id'])));
		}
		$this->set('friends', $friends);
	}

	/* 
	 * add the friend into table 'friends',
	 * delete the corresponding friend request from the table 'request_friends'
	 */
	function add_friends($id = null) {
		$user = $this->Session->read('user');
		$this->User->id = $id;
		$requestor = $this->User->read();
		if ($user['User']['id'] < $requestor['User']['id']) 
			$this->Friend->query("INSERT INTO friends (user1_id, user2_id) VALUES (" . $user['User']['id'] . ", " . $requestor['User']['id'] . ")", $cachequeries = false );
		else 
			$this->Friend->query("INSERT INTO friends (user1_id, user2_id) VALUES (" . $requestor['User']['id'] . ", " . $user['User']['id'] . ")", $cachequeries = false);
		$this->RequestFriend->query("DELETE FROM request_friends WHERE requestor_id = " . $requestor['User']['id'] . 
									" AND user_id = " . $user['User']['id'], $cachequeries = false);
			
		$this->Session->setFlash($requestor['User']['first_name'] . ' ' . $requestor['User']['last_name'] . ' has been added to your friends\' list', 'default', array('class' => 'success'));
		$this->redirect(array('controller'=>'friends', 'action' => 'friend_requests'));
	}
	
	function delete_friends($id = null) {
		$user = $this->Session->read('user');
		$user1_id = null;
		$user2_id = null;
		if ($user['User']['id'] < $id) {
			$user1_id = $user['User']['id'];
			$user2_id = $id;
		} else {
			$user1_id = $id;
			$user2_id = $user['User']['id'];
		}
	
		$this->Friend->query("DELETE FROM friends WHERE user1_id = " . $user1_id . " AND user2_id = " . $user2_id, $cachequeries = false);
		$this->redirect(array('controller'=>'friends', 'action' => 'index'));
	}
	

	function request_friends($id = null) {
		$requestor = $this->Session->read('user');
		$this->User->id = $id;
		$user = $this->User->read();
		$this->Friend->query("INSERT INTO request_friends (requestor_id, user_id) VALUES (" . $requestor['User']['id'] . ", " . $user['User']['id'] . ")");
		
		$this->Session->setFlash('The reuqest has been sent.', 'default', array('class' => 'success'));
		$this->redirect(array('controller'=>'users', 'action' => 'show_users'));
	}
	
	
	function friend_requests() {
		$user = $this->Session->read('user');
		$friend_requests = 
				$this->RequestFriend->find('all', array('conditions'=>array('RequestFriend.user_id' =>$user['User']['id'])));
		$data = array(
			'user' => $user,
			'friend_requests' => $friend_requests
		);
		$this->set($data);
	}
	
}
